﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SJZD_TypeManage : ATPages
{
    ZSql zsql = new ZSql();
    
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hfxs.Value = Request.QueryString["moduid"].ToString();
            lblwz.Text = f.getModuleidAdd(hfxs.Value);

            gdata();
            initData();
            
        }
    }
   
    protected void lnkbtnEdit_Click(object sender, EventArgs e)
    {
        string WTId = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("WorkTitleAdd.aspx?WTId=" + WTId + "&moduid=" + hfxs.Value + "&action=update");
       
    }
    protected void lnkbtnFlag_Click(object sender, EventArgs e)
    {
        string WTId = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("WorkTitleAdd.aspx?WTId=" + WTId + "&moduid=" + hfxs.Value + "&action=flag");

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("WorkTitleAdd.aspx?moduid=" + hfxs.Value + "&action=add");
    }

    protected void btndel_Click(object sender, EventArgs e)
    {
        ZSql del = new ZSql();
        int n = 0;
        if (this.GvDataWT.Rows.Count > 0)
        {
            for (int i = 0; i < GvDataWT.Rows.Count; i++)
            {
                CheckBox ck = GvDataWT.Rows[i].Cells[7].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {

                    del.Open("delete CS_WorkTitle  where WTId=" + this.GvDataWT.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);

                }
                else
                {
                    n++;
                }

                if (n == this.GvDataWT.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }

            }
            }
        }

       protected void btnallflag_Click(object sender, EventArgs e)
    {
        ZSql allflag = new ZSql();
        int n = 0;
        if (this.GvDataWT.Rows.Count > 0)
        {
            for (int i = 0; i < GvDataWT.Rows.Count; i++)
            {
                CheckBox ck = GvDataWT.Rows[i].Cells[7].FindControl("chkDelete") as CheckBox;
                if (ck.Checked)
                {
                    int flag = 1;
                    allflag.Open("update CS_WorkTitle set AuditState=" + flag + " where WTId=" + this.GvDataWT.Rows[i].Cells[0].Text);
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('审核成功');", true);
                }
                else
                {
                    n++;
                }

                if (n == this.GvDataWT.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }


            }
        }
        
    }
   

    private void gdata() 
    {
        string sql = "select * ,(select count(0) from CS_Staff where a.WTId=StaffWork) as ProNum from CS_WorkTitle a order by WTId desc";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }

    private void initData()
    {
        hfuserId.Value = Request.Cookies["user"].Values["id"].ToString();
    }

    protected void btnSelec_Click(object sender, EventArgs e)
    {
        string search = "";
        if (!string.IsNullOrEmpty(txtselecName.Text))
        {
            search += " and WTitle like'%" + txtselecName.Text + "%'";
        }
        string sql = "select * ,(select count(0) from CS_Staff where a.WTId=StaffWork) as ProNum from CS_WorkTitle a  where 1=1 " + search + " order by WTId desc";
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
                 
    }
   
    protected void GvDataWT_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        f.checkLevels(Request.Cookies["user"].Values["roleid"], hfxs.Value, 4, GvDataWT, btndel, btnallflag, btnAdd);
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string AuditState = DataBinder.Eval(e.Row.DataItem, "AuditState").ToString();

            if (AuditState == "1")
            {
                e.Row.Cells[6].Text = "已通过";
                e.Row.Cells[6].ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            if (AuditState == "0")
            {
                e.Row.Cells[6].Text = "未通过";
                e.Row.Cells[6].ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            if (AuditState == "")
            {
                e.Row.Cells[6].Text = "未审核";
                e.Row.Cells[6].ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            string ProNum = DataBinder.Eval(e.Row.DataItem, "ProNum").ToString();
            if (Convert.ToInt16(ProNum) > 0)
            {
                (e.Row.FindControl("chkDelete") as CheckBox).Enabled = false;
            }
        }
    }
}
